<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: article 
* Author			: 乔伟
* Create Time		: 2011-10-05 
* Modify Time		: 2011-10-05 
* Modify Profile	:  
******************************************************/
class article extends Admin_Controller {

	/******************************************************
	* Profile			: 列表
	* Author			: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/ 
	public function index()
	{
		$where = $this->Request();
		
		!isset($where['page_size']) && $where['page_size'] = 20; 
		$where['order_by'] = "id desc"; 
		$articles = $this->article_manager->get_list($where); 
		
		$page_size = 20; $page = isset($_GET['page'])?$_GET['page']:1;
		// p_r($articles);exit;
		$total_count = $this->article_manager->query_result->record_count;   
		$page_count = intval($total_count/$page_size) + 1;
		$pre_page = ($page - 1) < 1 ? 1 : ($page - 1);
		$next_page = ($page + 1) > $page_count ? $page_count : ($page + 1);
		
		$get_str = "";
		if(!isset($_GET))
		{
			$get_str = "?";
		}
		else
		{
			$tmp = array();
			foreach($_GET as $key=>$value)
			{
				if($key != "page")
				{
					$tmp[] = "$key=$value";
				} 
			}
			$get_str = "?".implode('&',$tmp)."&";
		}
			
		$sp_config = array(
			'base_url' => _U('article/index'.$get_str,true),
			'current_page'=>$page,
			'pre_page'=>$pre_page,
			'next_page'=>$next_page,
			'page_count'=>$page_count
		); 
 
		
		$params = array(
			'articles' => $articles , 
			'sp_config' => $sp_config
		);

		$this->load->view("admin/article/v_index",$params); 
	} 
	
	/******************************************************
	* Profile		: 编辑
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function edit()
	{
		//$id = ($id == null)?$this->input->post("id"):$id;
		$id = isset($_GET['id'])?$this->input->get("id"):null;
		if($id == null) // 添加 
		{
			$article = $this->build_class('article');
			if(isset($_GET['class_id']))
			{
				$article->class_id = $this->input->get('class_id'); 
			}
		}
		else
		{
			$article = $this->article_manager->get($id); 
		}
			
		if($this->is_postback())
		{
			$this->load->library('form_validation');
	
			if ($this->form_validation->run('article') == TRUE)
			{
				$article = $this->filter_post($_POST); 
	
				if($id == null)  // 添加
				{
					$article['contribute_time'] = Date('Y-m-d');  
					$this->article_manager->insert($article); 
				}
				else 
				{
					$this->article_manager->update($article,array('id'=>$id));
					// redirect(SELF."/article/index");
				}
				if(!empty($_GET['returnurl']))
				{
					$this->goto_url(urldecode($this->input->get('returnurl'))) ;
					exit;
				}
				$this->goto_last_index_page();
			}
		}
		
		$this->load->view("admin/article/v_form",$article);
	}
	
	
	/******************************************************
	 * Profile		: 删除
	* Author		: 乔伟
	* Create Time		: 2011-10-05
	* Modify Time		: 2011-10-05
	* Modify Profile	:
	******************************************************/
	public function del($id = null)
	{
		// id == null 表示批量删除
		//if($id == null)
			//{
			//	$id = $this->input->form('ids');
			//}
	
			//$this->db->delete('article',"id in($id)");
	
			//$this->goto_last_index_page();
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id))
		{
			$this->article_manager->delete($id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				$this->article_manager->delete($id) ;
			}
		}
		$this->goto_last_index_page();
	}

	/******************************************************
	* Profile		: 移动到
	* Author		: 宁秀娟
	* Create Time		: 2012-06-08 
	* Modify Time		: 2012-06-08
	* Modify Profile	:  
	******************************************************/
	public function moveto($class_id,$article_id=null)
	{
		$article_id = isset($_GET['id'])?$_GET['id']:$article_id;
		if(!empty($article_id))
		{
			$this->article_manager->moveto($class_id,$article_id) ;
		}
		// print_r($_POST);exit; 
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $article_id)
			{
				// echo $article_id . "-$class_id";
				$this->article_manager->moveto($class_id,$article_id) ;
			}
		}  
		$this->goto_last_index_page();
	}
}
